﻿using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;

namespace WebApp.cn.ashx
{
    public class shoppingCartHandler : IHttpHandler, IRequiresSessionState
    {

        readonly BasePage thePage = new BasePage();

        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members


        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {

            try
            {
                string user = "";
                if ((thePage.LoginUserMember == null) && (HttpContext.Current.Request.Cookies["shopping_cart_mac"] != null))
                {
                    user = HttpContext.Current.Request.Cookies["shopping_cart_mac"].Value;
                }
                else if (thePage.LoginUserMember != null)
                {
                    user = thePage.LoginUserMember.MemberId;
                }
                if (context.Request.QueryString["sid"] != null)
                {
                    if (context.Request.QueryString["sid"] == "all")
                    {
                        BaseFactory.theShoppingCart.DeleteShoppingCartByUser(user);
                    }
                    else
                    {
                        BaseFactory.theShoppingCart.Delete(Convert.ToInt32(context.Request.QueryString["sid"]));
                    }
                    context.Response.Redirect("shopping_cart.html");
                }
                int id = Convert.ToInt32(context.Request.Form["cid"]);
                int amount = Convert.ToInt32(context.Request.Form["amount"]);
                int num3 = BaseFactory.theShoppingCart.ModifyShoppingCartAmount(id, amount);
                StringBuilder builder = new StringBuilder();
                switch (num3)
                {
                    case 0:
                        builder.Append("0");
                        break;

                    case 1:
                        builder.Append("1");
                        break;
                }
                DataTable shoppingCartByUser = BaseFactory.theShoppingCart.GetShoppingCartByUser(user);
                builder.Append("〇<tr>");
                builder.AppendLine("<th>商品货号</th>");
                builder.AppendLine("<th>商品图片</th>");
                builder.AppendLine("<th>商品名称</th>");
                builder.AppendLine("<th>销售价格</th>");
                builder.AppendLine("<th>数量</th>");
                builder.AppendLine("<th>小计</th>");
                builder.AppendLine("<th>删除</th>");
                builder.AppendLine("</tr>");
                if (shoppingCartByUser.Rows.Count > 0)
                {
                    foreach (DataRow row in shoppingCartByUser.Rows)
                    {
                        builder.AppendLine("<tr>");
                        builder.AppendLine(string.Concat(new object[] { "<td><a href=\"/goods.html?id=", row["gId"], "\" target=\"_blank\">", row["goodsArtno"], "</a></td>" }));
                        DataTable goodGalleryByGoods = BaseFactory.theGoodGallery.GetGoodGalleryByGoods(Convert.ToInt32(row["gid"]));
                        builder.AppendLine(string.Concat(new object[] { "<td><a href=\"/goods.html?id=", row["gId"], "\" target=\"_blank\"><img src=\"", goodGalleryByGoods.Rows[0]["thumbImg"], "\" alt=\"", goodGalleryByGoods.Rows[0]["imgAlt"], "\" /></a></td>" }));
                        builder.AppendLine(string.Concat(new object[] { "<td><a href=\"/goods.html?id=", row["gId"], "\" target=\"_blank\">", row["goodsName"], "</a>" }));
                        DataTable table3 = BaseFactory.theGoodsEntitysSpecifications.FrontDeskGetGoodsEntitysSpecificationsByGoodsEntitys(Convert.ToInt32(row["geid"]));
                        for (int i = 0; i < table3.Rows.Count; i++)
                        {
                            builder.Append(string.Concat(new object[] { (i > 0) ? "&nbsp;&nbsp;" : "", table3.Rows[i]["specificationsName"], "：", table3.Rows[i]["specificationsValueName"] }));
                        }
                        builder.Append("</td>");
                        builder.AppendLine("<td>" + string.Format("{0:C}", Convert.ToDecimal(row["salePrice"])) + "</td>");
                        builder.AppendLine(string.Concat(new object[] { "<td><p class=\"number\"><input type=\"button\" value=\"-\" onclick=\"amountJian('", row["sid"], "');\" /><span><input id=\"amount", row["sid"], "\" type=\"text\" maxlength=\"5\" onchange=\"changeAmount('", row["sid"], "');\" value=\"", row["amount"], "\"  /></span><input type=\"button\" value=\"+\" onclick=\"amountJia('", row["sid"], "');\" /></p></td>" }));
                        builder.AppendLine("<td>" + string.Format("{0:C}", Convert.ToDecimal(row["total"])) + "</td>");
                        builder.AppendLine("<td><a href=\"javascript:\" onclick=\"customButton('确定要清空购物车吗？','确定','shoppingCartHandler.ashx?sid=" + row["sid"] + "','取消');\">删除</a></td>");
                        builder.AppendLine("</tr>");
                    }
                }
                else
                {
                    builder.AppendLine("<tr>");
                    builder.AppendLine("<td colspan=\"7\" style=\"text-align: center; color: #ff0000;\">购物车目前没有加入任何商品</td>");
                    builder.AppendLine("</tr>");
                }
                int num5 = 0;
                int num6 = 0;
                decimal num7 = 0M;
                DataTable table4 = BaseFactory.theShoppingCart.GetShoppingCartByUser(user);
                foreach (DataRow row in table4.Rows)
                {
                    num5 += Convert.ToInt32(row["giveIntegral"]) * Convert.ToInt32(row["amount"]);
                    num6 += Convert.ToInt32(row["amount"]);
                    num7 += Convert.ToDecimal(row["total"]);
                }
                builder.AppendLine(string.Concat(new object[] { "〇<span>商品数量总计：<strong>", num6, "</strong>件</span><span>赠送积分总计：<strong>", num5, "</strong>分</span><span>商品金额总数：<strong>", string.Format("{0:C}", num7), "</strong></span>" }));
                context.Response.Write(builder.ToString());
            }
            catch
            {
                context.Response.Write("");
            }
        }

        #endregion
    }
}
